package com.example.androidexample.animation;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.IdRes;

import com.example.androidexample.R;

public class AlphaAnimationBuilder implements AnimationBuilder, SeekBar.OnSeekBarChangeListener, View.OnClickListener {

    private final AnimationActivity activity;
    private float fromAlpha = 0.0f;
    private float toAlpha = 1.0f;
    private TextView tvAlphaFromValue;
    private TextView tvAlphaToValue;

    public AlphaAnimationBuilder(AnimationActivity activity) {
        this.activity = activity;
        resetAlphaValue();
        initView();
    }

    private void resetAlphaValue() {
        fromAlpha = 0.0f;
        toAlpha = 1.0f;
    }

    private void initView() {
        SeekBar skbAlphaFromValue = activity.findViewById(R.id.skb_alpha_from_value);
        skbAlphaFromValue.setOnSeekBarChangeListener(this);
        tvAlphaFromValue = activity.findViewById(R.id.tv_alpha_from_value);

        SeekBar skbAlphaToValue = activity.findViewById(R.id.skb_alpha_to_value);
        skbAlphaToValue.setOnSeekBarChangeListener(this);
        tvAlphaToValue = activity.findViewById(R.id.tv_alpha_to_value);

        Button bt_alpha = activity.findViewById(R.id.bt_alpha);
        bt_alpha.setOnClickListener(this);

    }

    @Override
    public Animation buildAnimation() {
        return new AlphaAnimation(fromAlpha,toAlpha);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        @IdRes
        int seekBarId = seekBar.getId();
        if (seekBarId == R.id.skb_alpha_from_value) {
            fromAlpha = progress / 100f;
            tvAlphaFromValue.setText(fromAlpha + "");
        } else if (seekBarId == R.id.skb_alpha_to_value) {
            toAlpha = progress / 100f;
            tvAlphaToValue.setText(toAlpha + "");
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onClick(View v) {
        activity.handleAction(AnimationBuilderType.ALPHA,this);
    }
}
